package xyz.wpxy.exception;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.wpxy.entity.R;

/**
 * @Description: 全局异常处理
 * @author: Peng Wei
 * @date:2023/8/31 17:49
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public R errorCustom(Exception e) {
        log.error("异常信息:{}", ExceptionUtils.getStackTrace(e));
        return R.error().message("服务器正忙");
    }

    @ExceptionHandler(ServiceException.class)
    public R errorService(ServiceException e) {
        log.error("异常信息:{}", ExceptionUtils.getStackTrace(e));
        //返回自定义异常中的字段:
        return R.error().code(e.getCode()).message(e.getErrorMsg());
    }


}
